<?php
namespace jy_lianfeeshop;
defined('BY_JYA') or exit('error');
/*
	分销插件工具类
*/
class model extends \plugin{
	public function checkagent(){
		global $_RQ;
		//扫码进入的
		if($_RQ['fromuser'] && $_RQ['action']=='myhome'){
			$this->session('sao_fromuser',$_RQ['fromuser']);
		}
		if(!$_SESSION['uid']){
			$return['ischecklogin']=model('jy_weishop/member')->checklogin('','','','',true);
			return $return;
		}
		$return['pluginset']=$this->setting;
		$return['team_tiaojians']=$this->team_tiaojians;
		if($_RQ['is_access'] && $_RQ['action']!='apply' && $_RQ['action'] !='myshop' && $_RQ['action'] !='info'){
			$member=$this->model('member')->checkmember('',true);
		}else{
			$member=$this->model('member')->checkmember('',false);
		}
		if($_SESSION['container']=='wxapp' && !$_SESSION['fromh5wxapp']){
			$iswxapp=1;
		}
		if(($_RQ['is_access'] || $iswxapp) && (!$member['is_agent'] || $member['stop']) && $_RQ['action'] !='myshop' && $_RQ['action'] !='apply'){
			exi('','','jy_lianfeeshop/apply/');
		}
		return $member;
	}
	public function checklogin(){
		cfc('core')->checklogin();
		$this->model('member')->register();
	}
	public function current_level_up_types(){
		return str_array($this->setting['level_up_types']);
	}
	public function current_daili_file_types(){
		return str_array($this->setting['daili_file']);
	}
	public function available_areas(){
		$areas=file_get_contents(ROOT_D.'/core/resource/area_town.txt');
		$areas=unserialize($areas);
		return $areas;
	}
	public function get_area_code($area){
		$address=$area['province'];
		if($area['city']){
			$address.=' '.$area['city'];
		}
		if($area['district']){
			$address.=' '.$area['district'];
		}
		if($area['street']){
			$address.=' '.$area['street'];
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
		if(!$area_code){
			$isup=pdo_count('core_area',array(),'code');
			if($isup==0){
				require ROOT_D.'/update/update_area.php';
			}
		}
		return $area_code;
	}
	/*
		如果开启联动模式；非联动分销商不能下单
	*/
	function check_buy($goods_id=0,$nomsg=false){
		if($nomsg){
			return false;
		}
		$goods=pdo_get('jy_weishop_goods',array('id'=>$goods_id,'uniacid'=>$_SESSION['uniacid']),array('id','api_data'));
		$api_data=json_decode($goods['api_data'],true);
		if($api_data['jy_lianfeeshop']){
			$member=pdo_get('jy_lianfeeshop_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'is_agent');
			if(!$member['is_agent']){
				exi('您不是代理分销商，无权购买该商品！','error');
			}
		}
		return true;
	}
	public function add_upfee(){
		$end_time=strtotime(date("Y-m",time()));
		//$end_time=time();
		$start_time = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));
		$this->month=date("Y-m",$start_time+200);
		$params=array('uniacid'=>$_SESSION['uniacid'],'createtime >'=>$end_time);
		$parameter=array(
			'params'=>$params,
			'limit'=>'1',
			'orderby'=>'id desc',
		);
		$list=$this->get_list('upfee',$parameter,$total);
		if(count($list)>0){
			exi('上月的分佣已经派发！','error');
		}
		$params=array('uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($start_time,$end_time));
		$maxlevel=$this->get('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id','level desc');
		if(!$maxlevel){
			exi('未设置任何等级！','error');
		}
		//全部分销商
		$members=$this->getall('member',array('level_id'=>$maxlevel['id'],'is_maxred'=>1,'is_agent'=>1,'uniacid'=>$_SESSION['uniacid']));
		$allfee=$this->getsum('feechi',$params,'money');
		if($allfee<1){
			exi('奖金池为空！','error');
		}
		$total=count($members);
		if($total<1){
			exi('满足条件的老板(上级)为空！','error');
		}
		$fee=$allfee/$total;
		$data=array(
			'allfee'=>$allfee,
			'fee'=>$fee,
			'total'=>$total,
		);
		foreach($members as $item){
			$this->xun_upfee($item,$data);
		}
		return true;
	}
	/*
		循环
	*/
	public function xun_upfee($member=array(),$data=array()){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$data['mid'],
			'allfee'=>$data['allfee'],
			'fee'=>$data['fee'],
			'total'=>$data['total'],
			'month'=>$this->month,
			'type'=>5,
			'status'=>0,
			'createtime'=>TIMESTAMP
		);
		$this->pdo_insert('upfee',$insert);
		$insert=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$data['mid'],
					'money'=>$data['fee'],
					'createtime'=>TIMESTAMP,
					'status'=>1,
					'type'=>5,
		);
		$this->pdo_insert('commission',$insert);
		return true;
	}
	/*
		奖金池
	*/
	public function create_feechi($order){
		if(!$this->setting['max_red']){
			return false;
		}
		$new_fee=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$order['mid'],
				'type'=>0,
				'ordersn'=>$order['ordersn'],
				'money'=>$this->setting['max_red'],
				'createtime'=>TIMESTAMP,
				'status'=>0,
				'isadd'=>1,
		);
		$this->insert('feechi',$new_fee);
		return true;
	}
}